<?php


namespace app\admin\pms\controller;


use app\admin\pms\logic\GoodsCateLogic;
use app\admin\pms\validate\GoodsCateValidate;
use app\common\controller\BaseAdminController;

/**
 * 商品分类
 * Class GoodsCateController
 * @package app\admin\pms\controller
 */
class GoodsCateController extends BaseAdminController
{
    protected function init()
    {
        parent::init();
        $this->logic = new GoodsCateLogic();
        $this->validate = new GoodsCateValidate();
    }

    public function list()
    {
        $where = [];
        $this->form['name'] && $where[] = ['name', 'like', "%{$this->form['name']}%"];
        $query = $this->logic->search($where);
        $query->order("sort desc");
        $data = $this->logic->getAll($query);
        return $this->success($data);
    }

    public function remove()
    {
        $id = input('id');
        $count = $this->logic->getCount(['pid' => $id]);
        if ($count > 0) {
            return $this->error("存在下级不可删除");
        }
        $this->logic->deleteByWhere($id);
        return $this->success();
    }

    public function cascaderList()
    {
        $where = [
            'status' => 1
        ];
        $data = $this->logic->getList($where, [
            'pid',
            'name as label',
            'id as value',
        ]);
        return $this->success($data);
    }

}